package com.cy.aircompressor.main.ui.systemsetting.fragment.child;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.RelativeLayout;

import androidx.core.content.ContextCompat;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.cy.aircompressor.R;
import com.cy.aircompressor.config.AirConfig;
import com.cy.aircompressor.databinding.AirChildFragmentFirstBinding;
import com.cy.aircompressor.databinding.ChildContainerSecondFragmentBinding;
import com.cy.aircompressor.databinding.ExecutiveMaintenanceFootBinding;
import com.cy.aircompressor.databinding.SystemPowerManagerHeadBinding;
import com.cy.aircompressor.databinding.SystemUserManagerHeadBinding;
import com.cy.aircompressor.entity.power.PowerStation;
import com.cy.aircompressor.entity.select.SelectDict;
import com.cy.aircompressor.entity.system.UserInfo;
import com.cy.aircompressor.main.ui.onlinemonitor.viewmodel.OnlineMonitorViewModel;
import com.cy.aircompressor.main.ui.systemsetting.provider.PowerManagerProvider;
import com.cy.aircompressor.main.ui.systemsetting.provider.UserManagerProvider;
import com.cy.aircompressor.main.ui.systemsetting.viewmodel.SystemSettingViewModel;
import com.cy.aircompressor.main.view.pop.AirDropdownPopupWindow;
import com.cy.aircompressor.main.view.pop.PageSizePopupWindow;
import com.drug.lib.util.ToastUtil;
import com.drug.lib.view.recycptr.DynamicAdpTypePool;
import com.drug.lib.view.recycptr.DynamicRecyclerAdapter;
import com.drug.lib.view.recycptr.bean.RowEntity;
import com.jakewharton.rxbinding2.view.RxView;
import com.net.model.base.Resource;
import com.net.model.base.fragment.BaseVMFragment;
import com.net.model.utils.Constant;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class PowerStationManagerFragment extends BaseVMFragment<ChildContainerSecondFragmentBinding, SystemSettingViewModel> {
    private Integer pageNum = 1;
    private Integer pageSize = 10;
    private Integer pageTotal = 1;
    private String name;

    public static PowerStationManagerFragment newInstance() {
        Bundle args = new Bundle();
        PowerStationManagerFragment fragment = new PowerStationManagerFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void dataChange() {
        mViewModel.getQueryPowerStationByPage().observe(this, new Observer<Resource<RowEntity<PowerStation>>>() {
            @Override
            public void onChanged(Resource<RowEntity<PowerStation>> rowEntityResource) {
                rowEntityResource.handler(new OnCallback<RowEntity<PowerStation>>() {
                    @Override
                    public void onSuccess(RowEntity<PowerStation> data) {
                        int remainder = data.getTotal() % pageSize;
                        int page = data.getTotal() / pageSize;
                        pageTotal = remainder > 0 ? (page + 1) : page;
                        footBinding.total.setText(String.format("共计%d页", pageTotal));
                        dynamicRecyclerAdapter.setItems(data.getList());
                        dynamicRecyclerAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(Integer code, String message) {
                        super.onFailure(code, message);
                        if (code == Constant.EMPTY_DATA_ERROR) {
                            ToastUtil.show("该电站未安装传感器");
                            dynamicRecyclerAdapter.setItems(new ArrayList<>());
                            dynamicRecyclerAdapter.notifyDataSetChanged();
                        }
                    }
                });
            }
        });
    }

    @Override
    protected ChildContainerSecondFragmentBinding getViewBinding() {
        return ChildContainerSecondFragmentBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        headView();
        footView();
        initRecyclerAdapter();
    }

    @Override
    protected void requestNetwork() {
        mViewModel.queryPowerStationByPage(pageNum, pageSize, name);
    }

    SystemPowerManagerHeadBinding headBinding;

    private void headView() {
        mBinding.contentView.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.box_color));
        mBinding.contentView.getDelegate().setCornerRadius(3);

        headBinding = SystemPowerManagerHeadBinding.inflate(getLayoutInflater());
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        mBinding.airBaseHead.addView(headBinding.getRoot(), layoutParams);
        headBinding.addBtn.setText("+ 新增");
        RxView.clicks(headBinding.addBtn)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {

                });
        RxView.clicks(headBinding.searchButton)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    name = headBinding.powerStationName.getText().toString().trim();
                    if (TextUtils.isEmpty(name)) return;
                    requestNetwork();
                });
    }

    ExecutiveMaintenanceFootBinding footBinding;

    private void footView() {
        footBinding = ExecutiveMaintenanceFootBinding.inflate(getLayoutInflater());
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        mBinding.airBaseBottom.addView(footBinding.getRoot(), layoutParams);
        footBinding.page.setText(String.valueOf(pageNum));
        footBinding.size.setText(String.format("%d条/页", pageSize));
        RxView.clicks(footBinding.minusBtn)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    if (pageNum == 1) return;
                    footBinding.page.setText(String.valueOf(--pageNum));
                    requestNetwork();
                });
        RxView.clicks(footBinding.addBtn)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    if (pageNum >= pageTotal) return;
                    footBinding.page.setText(String.valueOf(++pageNum));
                    requestNetwork();
                });
        RxView.clicks(footBinding.size)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    PageSizePopupWindow sizePopupWindow = new PageSizePopupWindow(mContext, footBinding.size);
                    sizePopupWindow.setDropdownCallBack(new PageSizePopupWindow.DropdownCallBack() {
                        @Override
                        public void select(Integer size) {
                            pageSize = size;
                            requestNetwork();
                        }
                    });
                    sizePopupWindow.showAsAbove(mContext, footBinding.size, sizePopupWindow);//显示PopupWindow小弹窗
                });
    }

    DynamicRecyclerAdapter dynamicRecyclerAdapter;

    private void initRecyclerAdapter() {
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mBinding.recyclerView.setLayoutManager(mLayoutManager);
        dynamicRecyclerAdapter = new DynamicRecyclerAdapter();
        DynamicAdpTypePool pool = new DynamicAdpTypePool();
        PowerManagerProvider powerManagerProvider = new PowerManagerProvider(mContext);
        pool.register(PowerStation.class, powerManagerProvider);
        dynamicRecyclerAdapter.registerAll(pool);
        mBinding.recyclerView.setAdapter(dynamicRecyclerAdapter);
    }
}
